package com.thiswhyme.stream.rabbit.consumer1.service;

import com.rabbitmq.client.Channel;
import com.thiswhyme.stream.rabbit.consumer1.channel.InputChannel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
 * @description:
 * @author: thiswhyme
 * @date: 2020/9/16
 */
@Slf4j
@Service
public class BizService {

    @StreamListener(InputChannel.INPUT_CHANNEL_LOG)
    public void onLogMessgae(String msg) {
        System.out.println("【consumer1】监听到INPUT_CHANNEL_LOG消息，内容：" + msg);
    }

    @StreamListener(InputChannel.INPUT_CHANNEL_PARTITION)
    public void onPartitionMessgae(String msg) {
        log.info("【consumer1】监听到INPUT_CHANNEL_PARTITION消息，内容：{}", msg);
    }

    @StreamListener(InputChannel.INPUT_CHANNEL_MANUAL)
    public void onMessage(String message, @Header(AmqpHeaders.CHANNEL) Channel channel,
                          @Header(AmqpHeaders.DELIVERY_TAG) Long deliveryTag) {
        log.info("DemoReceiver.INPUT_CHANNEL_MANUAL消息，消息被消费，内容：{}", message);
        try {
            channel.basicAck(deliveryTag, false);
        } catch (IOException e) {
            log.warn("DemoReceiver.INPUT_CHANNEL_MANUAL消息，消息ACK失败，内容：{}", message);
        }
    }
}
